package com.tczs.web.config;

import org.apache.log4j.Logger;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class ConfigData {
    private static Logger logger = Logger.getLogger(ConfigData.class);

    private int port;
    private boolean openIpLimit;
    private String ipBlack;
    private String databaseIp;
    private Integer databasePort;

    private static ConfigData configData;
    private ConfigData(){
        loadConfig();
        logger.info("config load success: "+this.toString());
    }

    public static ConfigData getConfigData(){
        if(configData == null){
            synchronized (ConfigData.class){
                if(configData == null){
                    configData = new ConfigData();
                }
            }
        }
        return configData;
    }

    public void reLoadConfig(){
        logger.info("re load config  ---------");
        configData = null;
        getConfigData();
    }

    public void loadConfig(){
        Properties properties = new Properties();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader("./config.properties"));
            properties.load(bufferedReader);
        } catch (IOException e) {
            logger.error("load config is error",e);
        }
        this.port = Integer.parseInt(properties.getProperty("port"));
        this.openIpLimit = Boolean.parseBoolean(properties.getProperty("openIpLimit"));
        this.ipBlack = properties.getProperty("ipBlack");
        this.databaseIp = properties.getProperty("databaseIp");
        this.databasePort = Integer.parseInt(properties.getProperty("databasePort"));
    }

    public int getPort(){
        return port;
    }
    public boolean isOpenIpLimit() {
        return openIpLimit;
    }
    public String getIpBlack() {
        return ipBlack;
    }
    public String getDatabaseIp() {
        return databaseIp;
    }

    public Integer getGetDatabasePort() {
        return databasePort;
    }

    @Override
    public String toString() {
        return "ConfigData{" +
                "port=" + port +
                ", openIpLimit=" + openIpLimit +
                ", ipBlack='" + ipBlack +
                ", databaseIp=" + databaseIp +
                ", databasePort=" + databasePort +'\'' +
                '}';
    }
}
